home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Console Thyself / CDate / CDate.c next >
Encoding:
C/C++ Source or Header  |  2001-01-15  |  770 b   |  35 lines

  1. //-----------------------------------
  2. // CDate.c ⌐ 2001 by Charles Petzold
  3. //-----------------------------------
  4. #include <stdio.h>
  5.  
  6. struct Date
  7. {
  8.     int year;
  9.     int month;
  10.     int day;
  11. };
  12. int IsLeapYear(int year)
  13. {
  14.     return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
  15. }
  16. int DayOfYear(struct Date date)
  17. {
  18.      static int MonthDays[12] = {   0,  31,  59,  90, 120, 151,
  19.                                   181, 212, 243, 273, 304, 334 };
  20.  
  21.      return MonthDays[date.month - 1] + date.day +
  22.                             ((date.month > 2) && IsLeapYear(date.year));
  23. }
  24. int main(void)
  25. {
  26.     struct Date mydate;
  27.  
  28.     mydate.month = 8;
  29.     mydate.day   = 29;
  30.     mydate.year  = 2001;
  31.  
  32.     printf("Day of year = %i\n", DayOfYear(mydate));
  33.  
  34.     return 0;
  35. }